﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace Utils.Helpers
{
    public class EmailHelper
    {
        public static bool SendEmailHasCCAndBCCWithAttachment(List<string> toAddresses, List<string> mailCcs, List<string> mailBCcs, string subject, string body, List<System.Net.Mail.Attachment> attachments)
        {
            try
            {
                var fromAddress = new MailAddress("tphuongsmtpserver@gmail.com", "Nguyen Phuong");
                // var toAddress = new MailAddress(ToAddress, "To Name");
                const string fromPassword = "1234@1234";


                #region Intitial data
                string ToAddress = string.Empty;//mailTo != null ? string.Join(",", mailTo) : null;
                if (toAddresses != null && toAddresses.Count > 0)
                {
                    foreach (string toadd in toAddresses)
                    {
                        ToAddress += toadd + ",";
                    }

                    ToAddress = ToAddress.Replace(ToAddress[ToAddress.Length - 1].ToString(), "");
                }
                string CCAddress = string.Empty;//mailTo != null ? string.Join(",", mailTo) : null;
                if (mailCcs != null && mailCcs.Count > 0)
                {
                    foreach (string ccadd in mailCcs)
                    {
                        CCAddress += ccadd + ",";
                    }

                    CCAddress = CCAddress.Replace(CCAddress[CCAddress.Length - 1].ToString(), "");
                }

                string BCCAddress = string.Empty;//mailTo != null ? string.Join(",", mailTo) : null;
                if (mailBCcs != null && mailBCcs.Count > 0)
                {
                    foreach (string bccadd in mailBCcs)
                    {
                        BCCAddress += bccadd + ",";
                    }

                    BCCAddress = BCCAddress.Replace(BCCAddress[BCCAddress.Length - 1].ToString(), "");
                }
                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
                };
                #endregion

                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.From = fromAddress;

                if (!string.IsNullOrEmpty(ToAddress))
                {
                    mail.To.Add(ToAddress);
                }

                if (!string.IsNullOrEmpty(CCAddress))
                {
                    mail.CC.Add(CCAddress);
                }

                if (!string.IsNullOrEmpty(BCCAddress))
                {
                    mail.Bcc.Add(BCCAddress);
                }

                mail.Subject = subject;
                mail.Body = body.Replace(Environment.NewLine, "<BR>");
                mail.IsBodyHtml = true;

                if (attachments != null && attachments.Count > 0)
                {
                    foreach (System.Net.Mail.Attachment attachment in attachments)
                    {
                        mail.Attachments.Add(attachment);
                    }
                }
                smtp.Send(mail);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }
    }
}
